package com.llp.javaweb.service.impl;

import com.llp.javaweb.dao.ProductDAO;
import com.llp.javaweb.mapper.ProductMapper;
import com.llp.javaweb.pojo.AddProduct;
import com.llp.javaweb.pojo.Product;
import com.llp.javaweb.service.ProductService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @BelongsProject: AscentWeb
 * @BelongsPackage: com.llp.javaweb.service.impl
 * @Author: bovan
 * @Date: 2022/11/2 11:32
 * @Description:
 */
public class ProductServiceImpl implements ProductService {
    @Override
    public Boolean addProduct(HttpServletResponse response, AddProduct product) throws IOException {
        ProductMapper mapper = ProductDAO.getProducts();
        Product pro = mapper.selectByNumber(product.getProductnumber());
        //判断该商品是否是新商品
        if (product.getIsnewproduct().equals("0") && !Objects.isNull(pro)) {
            //该商品不是新商品,存在
            //存在，更新库存
            long stock = Long.parseLong(pro.getStock());
            long stock1 = Long.parseLong(product.getStock());
            int i = mapper.updateProducts(String.valueOf(stock + stock1), pro.getProductnumber());
            if (i == 1) {
                return true;
            } else {
                return false;
            }
        } else if(product.getIsnewproduct().equals("1") && Objects.isNull(pro)) {
            //该商品是新商品，不存在
            int flag = mapper.addProducts(product);
            if (flag == 1) {
                return true;
            } else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public List<Product> selectAllProducts() {
        ProductMapper mapper = ProductDAO.getProducts();
        List<Product> product = mapper.selectAllProducts();
        return product;
    }
}
